//
//  ChangeIDCardVC.m
//  Cunli
//
//  Created by lilubin on 15/6/30.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ChangeIDCardVC.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "Config.h"
#import "User.h"


@interface ChangeIDCardVC ()
@property (weak, nonatomic) IBOutlet UITextField *IDCardField;

@end

@implementation ChangeIDCardVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    if ([CLUser shareCLUser].idcard != nil) {
    if([Login curLoginDetailUser].idcard != nil){
//        _IDCardField.text = [CLUser shareCLUser].idcard;
        _IDCardField.text = [Login curLoginDetailUser].idcard;
        _IDCardField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    }
    self.navigationItem.title = @"身份证";
    [self setAllUIKitAttributes];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setAllUIKitAttributes
{
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 65, 30);
//    [rightBtn setBackgroundImage:[UIImage imageNamed:@"regnext"] forState:UIControlStateNormal];
    [rightBtn setTitle:@"确认" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = flexSpacer.width - 20;
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:flexSpacer, rightBarItem, nil]];
}

- (void)commitAction:(UIButton *)button
{
    [self.view endEditing:YES];
    if (_IDCardField.text.length != 0 && _IDCardField.text.length != 18) {
        [Config showHUDWithText:@"身份证不足18位" andImage:@"error"];
        return;
    }
    
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:4];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:_IDCardField.text forKey:@"value"];
    [CheckParams setObject:@"idcard" forKey:@"field"];
    
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    [MyDataService requestURL:@"edit_profile.php" httpMethod:@"POST" params:CheckParams fileDatas:nil complection:^(id result) {
//        NSLog(@"In IDCARD %@", result);
        if ([result[@"response"] isEqualToString:@"edit_profile"]) {
//            [CLUser shareCLUser].idcard = _IDCardField.text;
            [Login curLoginDetailUser].idcard = _IDCardField.text;
            [self.navigationController popViewControllerAnimated:YES];
        }
        else if([result[@"response"] isEqualToString:@"error"])
        {
            NSDictionary *dic = result[@"error"];
            [Config showHUDWithText:dic[@"text"] andImage:@"error"];
        }
    } failureAction:^{
        [Config showHUDWithText:@"姓名修改失败" andImage:@"error"];
    }];
    
}


@end
